﻿/*   
    Copyright (C) 2009 Galaktika Corporation ZAO

    This file is a part of Galaktika.BusinessMonitor
 
    Galaktika.BusinessMonitor is a free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
      
    You should have received a copy of the GNU General Public License
    along with Galaktika.BusinessMonitor.  If not, see <http://www.gnu.org/licenses/>.
  
    If GPL v.3 is not suitable for your products or company,
    Galaktika Corp provides Galaktika.BusinessMonitor under a flexible commercial license
    designed to meet your specific usage and distribution requirements.
    If you have already obtained a commercial license from Galaktika Corp,
    you can use this file under those license terms.
*/

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Net;
using System.Reflection;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Galaktika.BI.Charting;
using Galaktika.BI.Runtime.Services;
using Galaktika.BI.Silverlight.Controls;
using Galaktika.BI.Silverlight.Intermediate;
using Ranet.AgOlap.Controls.ToolBar;
using System.Windows.Controls.Primitives;
using Galaktika.BI.Extensibility;

namespace Galaktika.BI.Charting.Common
{
    public class ToolBarButtonItem : ToggleButton, IAssemblyResources, IBindableObject, IEditableObject
    {
        private ToolbarControl m_toolBar;

        #region Constructors
        public ToolBarButtonItem()
        {
            base.DefaultStyleKey = typeof(ToolBarButtonItem);
        }

        public ToolBarButtonItem(ToolbarControl toolBar)
        {
            this.m_toolBar = toolBar;
        }
        #endregion Constructors


        #region Public Properties

        public ToolbarControl ToolBar
        {
            get { return m_toolBar; }
            set { m_toolBar = value; }
        }

        public string AText { get; set; }    

        [DefaultValue(BorderStyles.Default)]
        public BorderStyles AButtonStyle { get; set; }       

        [DefaultValue(BarItemPaintStyle.Standard)]
        public BarItemPaintStyle APaintStyle { get; set; }     

        private bool m_ShowToolTips = true;
        
        public bool AShowToolTips
        {
            get
            {
                return this.m_ShowToolTips;
            }
            set
            {
                this.m_ShowToolTips = value;
            }
        }

        private string m_ToolTipText = "";
        
        [DefaultValue("")]
        public string AToolTipText
        {
            get
            {
                return this.m_ToolTipText;
            }
            set
            {
                this.m_ToolTipText = value;
            }
        }

        #region IAssemblyResources Members
        private string m_AssemblyName = string.Empty;
        [Browsable(false)]
        [DefaultValue("")]
        public string AssemblyName
        {
            get
            {
                return m_AssemblyName;
            }
            set
            {
                m_AssemblyName = value;
            }
        }
        #endregion IAssemblyResources Members

        private string m_AssemblyPush = string.Empty;
        [DefaultValue("")]
        public string AAssemblyPush
        {
            get
            {
                return this.m_AssemblyPush;
            }
            set
            {
                this.m_AssemblyPush = value;
                this.m_AssemblyName = value; // уже без этого, т.к. использую атрибут LookupPropertyAttribute
            }
        }

        private string m_ResNamePush = "";
        
        [DefaultValue("")]
        public string AResNamePush
        {
            get
            {
                return this.m_ResNamePush;
            }
            set
            {
                this.m_ResNamePush = value;
            }
        }

        private string m_AssemblyRelease =string.Empty;
        
        [DefaultValue("")]
        public string AAssemblyRelease
        {
            get
            {
                return this.m_AssemblyRelease;
            }
            set
            {
                this.m_AssemblyRelease = value;
                this.m_AssemblyName = value;
            }
        }

        private string m_ResNameRelease = string.Empty;
        
        [DefaultValue("")]
        public string AResNameRelease
        {
            get
            {
                return this.m_ResNameRelease;
            }
            set
            {
                this.m_ResNameRelease = value;
            }
        }


        #region Data Properties
        private bool m_Checked;
        [Browsable(false)]
        public bool AChecked
        {
            get
            {
                return this.m_Checked;
            }
            set
            {
                this.m_Checked = value;
                if (base.Dispatcher.CheckAccess())
                {
                    base.IsChecked = value;
                }
                else
                {
                    base.Dispatcher.BeginInvoke(() => base.IsChecked = value);
                }
            }
        }

        private string m_Variable = "";
        
        [DefaultValue("")]
        public string AVariable
        {
            get
            {
                return this.m_Variable;
            }
            set
            {
                this.m_Variable = value;
            }
        }

        private long m_ValueMask = 0;
        
        public long AValueMask
        {
            get
            {
                return this.m_ValueMask;
            }
            set
            {
                this.m_ValueMask = value;
            }
        }

        //private bool m_InGroup = false;
        [DefaultValue(false)]
        public bool AInGroup { get; set; }    
  
        #endregion Data Properties
        #endregion Public Properties


        #region Private Methods
        private Image LoadImage(string AssemblyName, string ResourceName)
        {
            if ((AssemblyName == string.Empty) || (ResourceName == string.Empty))
            {
                return null;
            }

            Assembly asm = null;
            try
            {
                asm = Assembly.Load(AssemblyName);
                if (asm == null)
                {
                    return null;
                }

                var im = new Image() { Stretch = Stretch.None };
                im.Source = UriResources.GetImage(ResourceName);
                return im;
            }
            catch
            {
                return null;
            }
        }
       
        #endregion Private Methods

        #region IBindableObject Members

        private IBindingContext m_Context;
        public IBindingContext Context
        {
            get
            {
                return m_Context;
            }
            set
            {
                m_Context = value;
            }
        }

        private Dictionary<string, IBindingManager> m_Bindings;
        public IDictionary<string, IBindingManager> Bindings
        {
            get
            {
                if (m_Bindings == null)
                {
                    m_Bindings = new Dictionary<string, IBindingManager>();
                }

                return m_Bindings;
            }
        }


        #endregion

        #region IEditableObject Members

        public void BeginEdit()
        {           
        }

        public void CancelEdit()
        {         
        }

        public void EndEdit()
        {
	        this.Name = this.AText;
            this.Visibility = Visibility.Visible;
	        //this.Content = this.m_Button;
            //this.m_Button.ButtonStyle = BarButtonStyle.Check;
            //this.m_Button.Border = BorderStyles.Default;
            //this.m_Button.PaintStyle = BarItemPaintStyle.Standard;
            //this.m_Button.AllowAllUp = true;
        }

        #endregion
    }

}
